//
//  ShellGrabOrderHeadView.m
//  ShellWallert
//
//  Created by lvzhao on 2019/10/1.
//  Copyright © 2019 VV. All rights reserved.
//

#import "ShellGrabOrderHeadView.h"
#import "ShellGrabOrderViewModel.h"
#import "ShellOrderViewController.h"

@interface  ShellGrabOrderHeadView()

@property (nonatomic,strong) ShellGrabOrderViewModel *viewModel;

//全网数据
@property (nonatomic,strong) NSMutableArray *allDataArray;

//账户
@property (nonatomic,strong) UILabel *balanceLabel;
//额度
@property (nonatomic,strong) UILabel *amountLabel;
//收益
@property (nonatomic,strong) UILabel *incomeLabel;

//账户状态
@property (nonatomic,strong) UIButton *accountStatueBtn;


@end
@implementation ShellGrabOrderHeadView

- (instancetype)initViewModel:(ShellGrabOrderViewModel *)viewModel
{
    self = [super init];
    if (self) {
        
        self.viewModel = (ShellGrabOrderViewModel *)viewModel;
        [self setupView];
    }
    return self;
}

#pragma mark - setupView
- (void)setupView{
    
    
    ///!!!:全网数据
    UILabel *dataLabel = [UILabel lz_labelWithText:LZLocalizedString(@"全网数据") fontSize:k_textFont(16) color:UIColorHex(0xFFFFFF)];
    [self addSubview:dataLabel];
    [dataLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(15);
        make.top.mas_equalTo(20);
        make.height.mas_equalTo(22);
        make.width.mas_equalTo(200);
    }];
    
    
    UIView *contentView = [[UIView alloc] init];
    contentView.backgroundColor = COLOR_TINT;
    [self addSubview:contentView];
    [contentView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(15);
        make.top.mas_equalTo(50);
        make.width.mas_equalTo(K_SCREENWIDTH - 30);
        make.height.mas_equalTo(222);
    }];
    
    
    ///数据
    NSArray *allData = @[LZLocalizedString(@"全网订单"),@"0",
                         LZLocalizedString(@"全网流水"),@"0 USDT",
                         LZLocalizedString(@"今日订单"),@"0",
                         LZLocalizedString(@"今日流水"),@"0 USDT",
                         LZLocalizedString(@"主网工作"),@"0",
                         LZLocalizedString(@"在线节点"),@"0"];
    
    //每个Item宽高
    CGFloat H = 20;
    CGFloat W = (K_SCREENWIDTH - 30 - 40 - 10)/2.0;
    //每行列数
    NSInteger rank = 2;
    //每列间距
    CGFloat rankMargin = 10;
    //每行间距
    CGFloat rowMargin = 6;
    //Item索引 ->根据需求改变索引
    NSUInteger index = allData.count;
    
    CGFloat top = 16;

    for (int i = 0 ; i< index; i++) {
        UILabel *detailLab = [UILabel lz_labelWithText:allData[i] fontSize:k_textFont(12) color:UIColorHex(0xFFFFFF)];
        //Item X轴
        CGFloat X = (i % rank) * (W + rankMargin) + 10;
        //Item Y轴
        NSUInteger Y = (i / rank) * (H +rowMargin);

        if(i > 3){
           Y += 20;
        }
        if(i > 7){
            Y += 20;
        }
        if(i%2 == 0){
            detailLab.textColor = UIColorHex(0x8D99B2);
            detailLab.font = k_textFont(12);
            W = (K_SCREENWIDTH - 30 - 40 - 10)/2.0  +10;

        } else {
            [detailLab lz_rightAlignment];
            detailLab.textColor = UIColorHex(0xFFFFFF);
            detailLab.font = k_textFont(14);
            W = (K_SCREENWIDTH - 30 - 40 - 10)/2.0  +10;
        }
        
        detailLab.frame = CGRectMake(X, Y + top, W, H);
        [contentView addSubview:detailLab];
        [self.allDataArray addObject:detailLab];
    }
    
    
    ///虚线
    UIImageView *line1 = [[UIImageView alloc] init];
    line1.image = k_imageName(@"icon_xuxian");
    [contentView addSubview:line1];
    [line1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(10);
        make.top.mas_equalTo(75);
        make.height.mas_equalTo(1);
        make.width.mas_equalTo(K_SCREENWIDTH - 50);
    }];
    
    UIImageView *line2 = [[UIImageView alloc] init];
    line2.image = k_imageName(@"icon_xuxian");
    [contentView addSubview:line2];
    [line2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(10);
        make.top.mas_equalTo(146);
        make.height.mas_equalTo(1);
        make.width.mas_equalTo(K_SCREENWIDTH - 50);
    }];
    
    ///!!!:账户
    UILabel *accountLabel = [UILabel lz_labelWithText:LZLocalizedString(@"账户") fontSize:k_textFont(16) color:UIColorHex(0xFFFFFF)];
    [self addSubview:accountLabel];
    [accountLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(15);
        make.top.mas_equalTo(contentView.mas_bottom).mas_offset(15);
        make.height.mas_equalTo(22);
        make.width.mas_equalTo(200);
    }];
    
    
    
    UIView *accountView = [[UIView alloc] init];
    accountView.backgroundColor = COLOR_TINT;
    [self addSubview:accountView];
    [accountView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(15);
        make.top.mas_equalTo(accountLabel.mas_bottom).mas_offset(8);
        make.width.mas_equalTo(K_SCREENWIDTH - 30);
        make.height.mas_equalTo(100);
    }];
    

    //余额
    [accountView addSubview:self.balanceLabel];
    [self.balanceLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(20);
        make.top.mas_equalTo(14);
        make.width.mas_equalTo(250);
        make.height.mas_equalTo(20);
    }];
    
    
    //额度
    [accountView addSubview:self.amountLabel];
    [self.amountLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(20);
        make.top.mas_equalTo(self.balanceLabel.mas_bottom).mas_offset(6);
        make.width.mas_equalTo(250);
        make.height.mas_equalTo(20);
    }];
    
    
    //余额
    [accountView addSubview:self.incomeLabel];
    [self.incomeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(20);
        make.top.mas_equalTo(self.amountLabel.mas_bottom).mas_offset(6);
        make.width.mas_equalTo(250);
        make.height.mas_equalTo(20);
    }];
    
    
    //金额
    NSString *balanceString = [NSString stringWithFormat:@"%@: %@",@"HKD",@"0"];
    NSMutableAttributedString *balanceAttributedString = [NSAttributedString attributedString:balanceString rangeTitle:LZLocalizedString(@"HKD") leftFont:k_textFont(14) rightFont:k_textFont(12) leftColor:UIColorHex(0xFFFFFF) rightColor:UIColorHex(0x8D99B2)];
    self.balanceLabel.attributedText = balanceAttributedString;
    
    
    //额度
    NSString *amountString = [NSString stringWithFormat:@"%@: %@", LZLocalizedString(@"额度"),@"0"];
    NSMutableAttributedString *amountAttributedString = [NSAttributedString attributedString:amountString rangeTitle:LZLocalizedString(@"额度") leftFont:k_textFont(14) rightFont:k_textFont(12) leftColor:UIColorHex(0xFFFFFF) rightColor:UIColorHex(0x8D99B2)];
    self.amountLabel.attributedText = amountAttributedString;
    
    //收益
    NSString *incomeString = [NSString stringWithFormat:@"%@: %@", LZLocalizedString(@"收益"),@"0"];
    NSMutableAttributedString *incomeAttributedString = [NSAttributedString attributedString:incomeString rangeTitle:LZLocalizedString(@"收益") leftFont:k_textFont(14) rightFont:k_textFont(12) leftColor:UIColorHex(0xFFFFFF) rightColor:UIColorHex(0x8D99B2)];
    self.incomeLabel.attributedText = incomeAttributedString;
    
    
    //账户状态
    [accountView addSubview:self.accountStatueBtn];
    [self.accountStatueBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(-20);
        make.centerY.mas_equalTo(accountView.mas_centerY);
        make.width.mas_equalTo(80);
        make.height.mas_equalTo(30);
    }];
    
    
    ///!!!:待完成的订单
    UILabel *orderLab = [UILabel lz_labelWithText:LZLocalizedString(@"待完成的订单") fontSize:k_textFont(16) color:UIColorHex(0xFFFFFF)];
    [self addSubview:orderLab];
    [orderLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(15);
        make.top.mas_equalTo(accountView.mas_bottom).mas_offset(15);
        make.height.mas_equalTo(22);
        make.width.mas_equalTo(160);
    }];
    
    ///!!!:全部订单
    UIButton *allBtn = [UIButton lz_buttonTitle:LZLocalizedString(@"全部") titleColor:UIColorHex(0x4AB2E7) fontSize:13];
    [allBtn setImage:k_imageName(@"ico_next_xiaojiantou") forState:UIControlStateNormal];    
    [self addSubview:allBtn];
    [allBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(-15);
        make.centerY.mas_equalTo(orderLab.mas_centerY);
        make.height.mas_equalTo(22);
        make.width.mas_equalTo(80);
    }];
    [allBtn SG_imagePositionStyle:SGImagePositionStyleRight spacing:3];

    
    [[allBtn rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
        ShellOrderViewController *orderVC = [[ShellOrderViewController alloc] init];
        [LZTool.currentViewController.navigationController pushViewController:orderVC animated:YES];
    }];
    
    
    @weakify(self);
    [self.viewModel.reloadSubject subscribeNext:^(id  _Nullable x) {
        @strongify(self);
        [self reloadRecordData:[x integerValue]];
    }];
    
    
    [[[NSNotificationCenter defaultCenter] rac_addObserverForName:k_APPLanguageWillChangeNotificationName object:nil] subscribeNext:^(NSNotification * _Nullable x) {
        @strongify(self);
        dataLabel.text = LZLocalizedString(@"全网数据");
        accountLabel.text = LZLocalizedString(@"账户");
        orderLab.text = LZLocalizedString(@"待完成的订单");
        [allBtn setTitle:LZLocalizedString(@"全部") forState:UIControlStateNormal];
        [allBtn SG_imagePositionStyle:SGImagePositionStyleRight spacing:3];

        [self.accountStatueBtn setTitle:LZLocalizedString(@"停止接单") forState:UIControlStateSelected];
        [self.accountStatueBtn setTitle:LZLocalizedString(@"开始接单") forState:UIControlStateNormal];

        //请求账户信息
        [self.viewModel  getReadAccount];
        [self.viewModel  getStatus];

        
    }];

}


- (void)reloadRecordData:(NSInteger)type{
    
    switch (type) {
          case 4:{
              self.accountStatueBtn.selected = self.viewModel.userStatus;
          }
              break;
            
            case 5:{

                ///数据
                NSString *orderAllAmount = [NSString stringWithFormat:@"%f",self.viewModel.dataModel.orderAllAmount].saveDecimal;
                NSString *orderTodayAmount = [NSString stringWithFormat:@"%f",self.viewModel.dataModel.orderTodayAmount].saveDecimal;

               NSArray *allData = @[LZLocalizedString(@"全网订单"),[NSString stringWithFormat:@"%ld",(long)self.viewModel.dataModel.orderAll],
                                    LZLocalizedString(@"全网流水"),[NSString stringWithFormat:@"%@ USDT",orderAllAmount],
                                    LZLocalizedString(@"今日订单"),[NSString stringWithFormat:@"%ld",(long)self.viewModel.dataModel.orderToday],
                                    LZLocalizedString(@"今日流水"),[NSString stringWithFormat:@"%@ USDT",orderTodayAmount],
                                    LZLocalizedString(@"主网工作"),[NSString stringWithFormat:@"%ld %@",self.viewModel.dataModel.hours,LZLocalizedString(@"小时")],
                                    LZLocalizedString(@"在线节点"),[NSString stringWithFormat:@"%ld",(long)self.viewModel.dataModel.onlineUser]];
                
                for(int i = 0; i < self.allDataArray.count; i++){
                    UILabel *itemLabel = self.allDataArray[i];
                    itemLabel.text = allData[i];
                }
            }
            
            break;
            case 6:{
                //金额
                
                
                NSString *currencyBalance = [NSString stringWithFormat:@"%f",self.viewModel.accountModel.currencyBalance].saveDecimal;
                 NSString *balanceString = [NSString stringWithFormat:@"%@: %@",self.viewModel.accountModel.currencyType,currencyBalance];
                 NSMutableAttributedString *balanceAttributedString = [NSAttributedString attributedString:balanceString rangeTitle:self.viewModel.accountModel.currencyType leftFont:k_textFont(14) rightFont:k_textFont(12) leftColor:UIColorHex(0xFFFFFF) rightColor:UIColorHex(0x8D99B2)];
                 self.balanceLabel.attributedText = balanceAttributedString;
                 
                 
                 //额度
                NSString *quota = [NSString stringWithFormat:@"%f",self.viewModel.accountModel.quota].saveDecimal;
                 NSString *amountString = [NSString stringWithFormat:@"%@: %@", LZLocalizedString(@"额度"),quota];
                 NSMutableAttributedString *amountAttributedString = [NSAttributedString attributedString:amountString rangeTitle:LZLocalizedString(@"额度") leftFont:k_textFont(14) rightFont:k_textFont(12) leftColor:UIColorHex(0xFFFFFF) rightColor:UIColorHex(0x8D99B2)];
                 self.amountLabel.attributedText = amountAttributedString;
                 
                 //收益
                 NSString *income = [NSString stringWithFormat:@"%f",self.viewModel.accountModel.profit].saveDecimal;
                 NSString *incomeString = [NSString stringWithFormat:@"%@: %@", LZLocalizedString(@"收益"),income];
                 NSMutableAttributedString *incomeAttributedString = [NSAttributedString attributedString:incomeString rangeTitle:LZLocalizedString(@"收益") leftFont:k_textFont(14) rightFont:k_textFont(12) leftColor:UIColorHex(0xFFFFFF) rightColor:UIColorHex(0x8D99B2)];
                 self.incomeLabel.attributedText = incomeAttributedString;
                         
                  
            }
            
            break;
            
          default:
              break;
      }
}



//切换状态
- (void)switchStatus:(UIButton *)sender{
    
    if(k_userToken.length == 0){
        ShellLoginViewController *login = [[ShellLoginViewController alloc] init];
        LZBaseNavigationController *nav = [[LZBaseNavigationController alloc] initWithRootViewController:login];
        nav.modalPresentationStyle = UIModalPresentationFullScreen;
        [LZTool.currentViewController.navigationController presentViewController:nav animated:YES completion:nil];
        return ;
    }

    if(!sender.selected){
        @weakify(self);
        [LZToolView showAlertType:LZAlertViewTypeTipAlter withTitle:LZLocalizedString(@"提示") message:self.viewModel.dataModel.textDesc cancel:@"" sure:LZLocalizedString(@"确定") objectDict:nil cancelBlock:^(id  _Nullable object) {} sureBlock:^(id  _Nullable object) {
            @strongify(self);
            [self.viewModel updateStatus:sender.selected ? @"off":@"on"];
        }];
    } else {
        [self.viewModel updateStatus:sender.selected ? @"off":@"on"];
    }
}


#pragma mark - get && set
- (NSMutableArray *)allDataArray{
    if(!_allDataArray){
        _allDataArray = [NSMutableArray arrayWithCapacity:0];
    }
    return _allDataArray;
}


//余额
- (UILabel *)balanceLabel{
    if(!_balanceLabel){
        _balanceLabel = [UILabel lz_labelWithText:@"" fontSize:k_textFont(12) color:UIColorHex(0x8D99B2)];
    }
    return _balanceLabel;
}
//额度
- (UILabel *)amountLabel{
    if(!_amountLabel){
        _amountLabel = [UILabel lz_labelWithText:@"" fontSize:k_textFont(12) color:UIColorHex(0x8D99B2)];
    }
    return _amountLabel;
}


//收益
- (UILabel *)incomeLabel{
    if(!_incomeLabel){
        _incomeLabel = [UILabel lz_labelWithText:@"" fontSize:k_textFont(12) color:UIColorHex(0x8D99B2)];
    }
    return _incomeLabel;
}

//账户状态
- (UIButton *)accountStatueBtn{
    if(!_accountStatueBtn){
        _accountStatueBtn = [UIButton lz_buttonTitle:LZLocalizedString(@"开始接单") titleColor:UIColorHex(0xFFFFFF) fontSize:13];
        
        [_accountStatueBtn setTitle:LZLocalizedString(@"停止接单") forState:UIControlStateSelected];
        UIImage *normalImage = [UIImage gradientImageWithColors:@[UIColorHex(0x65DFFE),UIColorHex(0x2C6FE)] rect:CGRectMake(0, 0, 80, 30)];
        UIImage *selectedImage = [UIImage gradientImageWithColors:@[UIColorHex(0xFA68A6),UIColorHex(0xF4376D)] rect:CGRectMake(0, 0, 80, 30)];

        [_accountStatueBtn setBackgroundImage:normalImage forState:UIControlStateNormal];
        [_accountStatueBtn setBackgroundImage:selectedImage forState:UIControlStateSelected];
        [_accountStatueBtn addTarget:self action:@selector(switchStatus:) forControlEvents:UIControlEventTouchUpInside];
        _accountStatueBtn.cornerRadius = 6;
        _accountStatueBtn.titleLabel.numberOfLines = 0;
        _accountStatueBtn.titleLabel.adjustsFontSizeToFitWidth = YES;
    }
    return _accountStatueBtn;
}
@end
